package SystemB;

import Alarms.AbstractAlarm;
import Alarms.SimulationAlarm;


public class FireAlarm extends SimulationAlarm {
	
	public FireAlarm(String name, int Xpos, int Ypos, int mwWidth,
					 String alarmId, String simulationNormalMessage,
					 String simulationEmergencyMessage, int subSystemHaltEnvId, String sensor_name) {
		super(name, Xpos, Ypos, mwWidth, alarmId, simulationNormalMessage, simulationEmergencyMessage, subSystemHaltEnvId, sensor_name);
		
		this.status = 0;
	}
	
	protected void initialPostPhase(){
		switch (status) {
		case 0: 
			if (status != previousStatus) {
				previousStatus = status;
				mw.WriteMessage("Alarm: [" + alarmId + "] Status: disarmed");
			}
			break;
		case 1:
			if (status != previousStatus) {
				previousStatus = status;
				mw.WriteMessage("Alarm: [" + alarmId + "] Status: armed / normal");
			}
			break;
		case 2:
			if (status != previousStatus) {
				previousStatus = status;
				mw.WriteMessage("Alarm: [" + alarmId + "] Status: !!! EMERGENCY !!!");
			}
			break;
		default:
			mw.WriteMessage("Alarm: [" + alarmId + "] ERROR: Unrecognized status = " + status);
		}
	}
	
	protected void handlePhase(){
		
		if (Evt.GetEventId() == subSystemHaltEnvId) {
			halt();
		}
		
		if (Evt.GetEventId() == monitorPingEvtId) {
			postEvent(monitorPingReturnEvtId, alarmId + separator + name + separator + status);
		}

		if (Evt.GetEventId() == armDisarmEvtId) {
			String evtMsg = Evt.GetMessage();
			String evtAlarmId = evtMsg.substring(0, evtMsg.length() - 1);
			char evtCommand = evtMsg.charAt(evtMsg.length() - 1);
			
			if (evtAlarmId.equals(alarmId)) { 
				if (evtCommand == '1')  {
					if (status != 1) status = 1;
					postEvent(confirmationArmDisarmEvtId, evtMsg);
				}
			}

			if (evtAlarmId.equals(alarmId)) { 
				if (evtCommand == '0')  {
					if (status != 0) status = 0;
					postEvent(confirmationArmDisarmEvtId, evtMsg);
				}
			}
		}
		
		customEventHandler();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AbstractAlarm alarm = new FireAlarm("Fire Alarm", 0, 352, 550, "FA", "FA0", "FA1", 91, "Fire Alarm");

		alarm.run(args);
	}

}